#coding:utf-8
import jieba
'''
查询某个文件中出现次数比较多的15个词语，并显示出来
ABC 30
BCD 25
XXX 19
'''
txt = open('英雄联盟.txt','r',encoding='UTF-8').read()
words = jieba.lcut(txt)#对文本内容进行分词
counts = {} #创建保存词语和出现次数的字典
for word in words:
    #单个字不算，数字不算
    try:
        if (len(word)==1) or (isinstance(int(word),int)):
            pass
    except:
        counts[word] = counts.get(word,0)+1
#将键值对转换为列表，字典中的key+value转换为元组
#[(key,value),(key,value)]
items = list(counts.items())
#print(items)
#根据出现的词语的次数排序
#sort(key,reverse) lambda隐函数 x:x[1]对某个对象的第几个元素进行排列
items.sort(key=lambda x:x[1],reverse=True)
for i in range(25):
    word,count=items[i]
    #  :<左对齐占5位   :>5右对齐占5位
    print('{0:<5}{1:>5}'.format(word,count))
